import router from "./router";
import store from "./store";


router.beforeEach((to,from,next)=>{
    let token = store.state.token;
    let userInfo = store.state.userInfo

    if(!token){
        if(to.path == '/login'){
            next();
        }else{
            next('/login');
        }
    }else{
        if(to.path == '/login'){
            next(from.path)
        }else{
            if(!userInfo){
                store.dispatch('getUserInfo').then(res=>{
                    if(res.data.flag){
                        next()
                    }else{
                        next('/login')
                    }
                }).catch(error=>{
                    console.log(error);
                })
            }else{
                next();
            }
        }
    }
})